package com.learn.demo.service.impl;

import com.learn.demo.config.RabbitMQDeadLetterConfig;
import com.learn.demo.service.DeadLetterMsgProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * 死信消息发送实现类
 *
 * @author banjiawei
 * @date 2021/12/12
 */
@Component
@Slf4j
public class DeadLetterMsgProviderImpl implements DeadLetterMsgProvider {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public String send() {
        String serial = UUID.randomUUID().toString();
        //id + 时间戳 全局唯一
        CorrelationData correlationData = new CorrelationData("9527"+new Date());
        //发送消息时指定 header 延迟时间
        rabbitTemplate.convertAndSend(RabbitMQDeadLetterConfig.GENERAL_EXCHANGE, RabbitMQDeadLetterConfig.GENERAL_KEY, serial, correlationData);
        log.info(String.format("==================%s====================", serial));
        return serial;
    }
}
